#include "dlgedittag.h"
#include "pm/manager.h"
#include "main.h"

DlgEditTag::DlgEditTag(wxWindow* parent, const wxString& title)
: DlgEditTagGui(parent, wxID_ANY, title)
{
}

void DlgEditTag::OnSubmit( wxCommandEvent& WXUNUSED(event) )
{
	m_tag.name(m_editName->GetValue());
	m_tag.colour(m_cpColour->GetColour());
	
	EndModal(Submit() ? wxID_OK : wxID_CANCEL);
}

DlgNewTag::DlgNewTag(wxWindow* parent)
: DlgEditTag(parent, _("New tag"))
{
}

bool DlgNewTag::Submit()
{
	if (pm::insert(m_tag) == false) {
		wxLogError(_("Can't insert tag !"));
		return false;
	}
	
	pmEvent<pm::Tag> evt(pmEVT_TAG_ADDED, m_tag);
	wxPostEvent(wxTheApp, evt);
	return true;
}

DlgUpdateTag::DlgUpdateTag(wxWindow* parent, const pm::Tag& tag)
: DlgEditTag(parent, _("Update tag"))
{
	m_tag = tag;
	
	m_editName->SetValue(m_tag.name());
	m_cpColour->SetColour(m_tag.colour());
}

bool DlgUpdateTag::Submit()
{
	if (pm::update(m_tag) == false) {
		wxLogError(_("Can't update tag !"));
		return false;
	}
	
	pmEvent<pm::Tag> evt(pmEVT_TAG_UPDATED, m_tag);
	wxPostEvent(wxTheApp, evt);
	return true;
}
